package com.hao.netty.chapter07;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.msgpack.MessagePack;

/**
 * @Description //TODO
 * @Date 2020/1/12
 * @Author songhao
 **/
public class MsgpackEncoder extends MessageToByteEncoder<Object> {

    @Override
    protected void encode(ChannelHandlerContext ctx,Object msg,ByteBuf bytebuf) throws Exception{
        try {
            MessagePack messagePack = new MessagePack();
            byte[] raw = messagePack.write(msg);
            bytebuf.writeBytes(raw);
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }
}
